package cn.yonyong.base.project.service.impl;

import cn.yonyong.base.project.entity.TblBatchappear;
import cn.yonyong.base.project.mapper.TblBatchappearMapper;
import cn.yonyong.base.project.service.BatchAppearService;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * @Describtion Todo
 * @Author yonyong
 * @Date 2019/9/6 17:08
 * @Version 1.0.0
 **/
@Service
@Transactional(readOnly = true)
public class BatchAppearServiceImpl implements BatchAppearService {

    @Resource
    private TblBatchappearMapper tblBatchappearMapper;

    @Override
    public List<TblBatchappear> getAllLists(int offset,int limit){
        RowBounds rowBounds = new RowBounds(offset,limit);
        List<TblBatchappear> list = tblBatchappearMapper.selectByRowBounds(new TblBatchappear(),rowBounds);
//        List<TblBatchappear> list = tblBatchappearMapper.selectAll();
        return  list;
    }

    @Override
    public List<TblBatchappear> getListByExample(TblBatchappear tblBatchappear,String minTime,String maxTime) {
        Example example = new Example(TblBatchappear.class);

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

        example.setOrderByClause("createTime DESC");
        example.setOrderByClause("id Desc");
        example.createCriteria()
                .andEqualTo("state",tblBatchappear.getState())      //这里的property为mybatis-generrator生成的实体类的属性，切记，不是数据库表的字段！！！！
                .andLike("batch",tblBatchappear.getBatch())
                .andLike("applyName",tblBatchappear.getApplyName())
                .andLike("commen2",tblBatchappear.getCommen2())
                .andIsNotNull("userid");

        try {
            //关于日期的比较，数据库里的时间字段可以直接使用between，但前提是，传入的必须是java.util.Date类型的值
            example.getOredCriteria().get(0)
                    .andBetween("createtime",formatter.parse(minTime),formatter.parse(maxTime));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        List<TblBatchappear> list = tblBatchappearMapper.selectByExample(example);
        return list;
    }
}
